using CoreManager.Models.Entities;
using CoreManager.Repository.DbContext;
using System.Linq.Expressions;

namespace CoreManager.Repository.Repositories
{
    /// <summary>
    /// 通用仓储接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T> where T : BaseEntity, new()
    {
        /// <summary>
        /// 获取数据库上下文
        /// </summary>
        CoreManagerDbContext Context { get; }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T?> GetByIdAsync(int id);

        /// <summary>
        /// 根据条件获取单个实体
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<T?> GetAsync(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns></returns>
        Task<List<T>> GetAllAsync();

        /// <summary>
        /// 根据条件获取实体列表
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<List<T>> GetListAsync(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        Task<(List<T> Items, int Total)> GetPagedListAsync(
            Expression<Func<T, bool>>? expression = null,
            int pageIndex = 1,
            int pageSize = 20,
            Expression<Func<T, object>>? orderBy = null,
            bool isAsc = true);

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> InsertAsync(T entity);

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<int> InsertRangeAsync(List<T> entities);

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(T entity);

        /// <summary>
        /// 根据条件更新
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="updateExpression"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(Expression<Func<T, bool>> expression, Expression<Func<T, T>> updateExpression);

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(int id);

        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> SoftDeleteAsync(int id);

        /// <summary>
        /// 检查是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<bool> ExistsAsync(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 获取数量
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<int> CountAsync(Expression<Func<T, bool>>? expression = null);

        /// <summary>
        /// 获取可查询对象
        /// </summary>
        /// <returns></returns>
        IQueryable<T> GetQueryable();
    }
}